﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlServerCe;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

namespace TcomInvoice
{
    public partial class ViewRpt : Form
    {
        private CrystalReport1 rpt = new CrystalReport1();

        public DateTime InvoiceDate { get; set; }
        public string TComAccount { get; set; }

        public string CustomerName { get; set; }
        public string Organization { get; set; }
        public string TaxCode { get; set; }
        public string Address { get; set; }
        public string PayMethod { get; set; }
        public string Account { get; set; }

        public double SumAmount { get; set; }
        public double VATPercent { get; set; }
        public double VATAmount { get; set; }
        public double TotalAmount { get; set; }
        public double AmountInWord { get; set; }

        public int InvoiceID { get; set; }

        public ViewRpt()
        {
            InitializeComponent();

            InvoiceDate = new DateTime(1900, 1, 1); //Null Date
        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
            PrepareReportData(rpt);
            this.crystalReportViewer1.ReportSource = rpt; 
        }

        private void PrepareReportData(ReportClass rpt)
        {
            DataSet1 ds = new DataSet1();
            string queryInvoiceLine = "SELECT * FROM InvoiceLine WHERE InvoiceID = " + InvoiceID.ToString() + " ";
            SqlCeDataAdapter adpt = new SqlCeDataAdapter(queryInvoiceLine, new SqlCeConnection { ConnectionString = @"Data Source=|DataDirectory|\tcomdb.sdf" });
            adpt.Fill(ds.Tables["InvoiceLine"]);

            //Bổ sung dòng trống trong trường hợp không đủ số dòng cần in
            for (int i = ds.Tables["InvoiceLine"].Rows.Count; i < 10 ; i++)
            {
                DataRow row = ds.Tables["InvoiceLine"].NewRow();
                row["InvoiceID"] = InvoiceID;
                row["LineNo"] = 0;
                row["ItemName"] = DBNull.Value;
                row["UnitName"] = DBNull.Value;
                row["Quantity"] = DBNull.Value;
                row["Price"] = DBNull.Value;
                row["Amount"] = DBNull.Value;

                ds.Tables["InvoiceLine"].Rows.Add(row);
            }
            rpt.SetDataSource(ds.Tables["InvoiceLine"]);

            SetParamValue(rpt, "InvoiceDate", InvoiceDate);
            SetParamValue(rpt, "TComAccount", TComAccount ?? "");
            SetParamValue(rpt, "CustomerName", CustomerName ?? "");
            SetParamValue(rpt, "Organization", Organization ?? "");
            SetParamValue(rpt, "TaxCode", TaxCode ?? "");
            SetParamValue(rpt, "Address", Address ?? "");
            SetParamValue(rpt, "PayMethod", PayMethod ?? "");
            SetParamValue(rpt, "Account", Account ?? "");

            SetParamValue(rpt, "SumAmount", SumAmount);
            SetParamValue(rpt, "VATPercent", VATPercent);
            SetParamValue(rpt, "VATAmount", VATAmount);
            SetParamValue(rpt, "TotalAmount", TotalAmount);
            string amountInWord = Utility.NumberInWord(TotalAmount);
            if (!string.IsNullOrEmpty(amountInWord))
            {
                SetParamValue(rpt, "AmountInWord", amountInWord + "đồng");
            }
            else
            {
                SetParamValue(rpt, "AmountInWord", string.Empty);
            }
        }

        private void SetParamValue(ReportClass rpt, string paramName, object paramValue)
        {
            ParameterFieldDefinition pfd = rpt.DataDefinition.ParameterFields[paramName];
            ParameterValues pv = pfd.CurrentValues;
            ParameterDiscreteValue pdv = new ParameterDiscreteValue();
            pdv.Value = paramValue;
            pv.Add(pdv);
            pfd.ApplyCurrentValues(pv);
        }

        private void ViewRpt_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.rpt.Close();
            this.rpt.Dispose();
            this.Dispose();
        }
    }
}
